package yh.thread.threadpoolcreate;

import yh.thread.threadcreate.MyCustomThread;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 使用Executors 来创建线程池
 * @author: zyh
 * @date: 2024/10/18 15:40
 **/
public class ExecutorsCreateTest {


    public void threadPoolCreate(){


        ExecutorService executorService = Executors.newFixedThreadPool(5);

        executorService.execute(()->{
            System.out.println("任务执行开始");
        });

        ExecutorService executorService1 = Executors.newSingleThreadExecutor();



        //可缓存的线程池
        ExecutorService executorService2 = Executors.newCachedThreadPool();
        Executors.newCachedThreadPool(new MyCustomThread("xxx业务线程"));
        executorService2.execute(()->{
            System.out.println("任务开始执行");
        });


        //线程延迟2小时执行
        ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
        scheduledExecutorService.schedule(() ->{
            System.out.println("延迟小时执行");
        },2, TimeUnit.HOURS);


        ExecutorService executorService3 = Executors.newWorkStealingPool();
        executorService3.execute(()->{
            System.out.println("任务执行中");
        });


    }

}
